import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  BadRequestException,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { CreateAuthDto } from './dto/create-auth.dto';
import { UpdateAuthDto } from './dto/update-auth.dto';
import { Public } from 'src/common/public.decorator';

@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  // 注册
  @Public()
  @Post('signup')
  signup(
    @Body() signupData: { username: string; password: string; phone: string },
  ) {
    console.log(signupData, 'signupData');

    if (!signupData.username) {
      throw new BadRequestException('缺少必填参数: username');
    } else if (!signupData.password) {
      throw new BadRequestException('缺少必填参数: password');
    } else if (!signupData.phone) {
      throw new BadRequestException('缺少必填参数: phone');
    }
    return this.authService.signup(signupData);
  }

  // 登录
  @Public()
  @Post('login')
  login(@Body() loginData: { username: string; password: string }) {
    return this.authService.login(loginData);
  }

  @Post()
  create(@Body() createAuthDto: CreateAuthDto) {
    return this.authService.create(createAuthDto);
  }

  @Get()
  findAll() {
    return this.authService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.authService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateAuthDto: UpdateAuthDto) {
    return this.authService.update(+id, updateAuthDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.authService.remove(+id);
  }
}
